<?php
/*
Plugin Name: Synergy Rules
Plugin URI: http://
Description: Plugin is counting bots visits
Author: Jaspreet Kaur Deol, Maia Engeli
Version: 1.0
Author URI: http://ditio.net
*/

function widget_syn_rules() {
?>
	
<?php

echo $before_widget; 
 	//echo $before_title . 'Rules' . $after_title;
global $wpdb;


	echo "<h4>Rules</h4>\n";
	$result = $wpdb->get_results("SELECT `TEXT` FROM `{$wpdb->prefix}user_rules` ORDER BY `id`"); 
	echo "<ol style=\"padding-left:16px\">";
	foreach ($result as $r) {
		echo ("<li><p>". $r->TEXT . "</p></li>\n");
		}
	 echo "</ol>";

	echo $after_widget; 
}

register_sidebar_widget('Rules', 'widget_syn_rules');

function rules_install()
{
    global $wpdb;
    $table = $wpdb->prefix."user_rules";
	
	$structure = "DROP TABLE $table;";
   $wpdb->query($structure);

    $structure = "CREATE TABLE $table (
        id INT(9) NOT NULL AUTO_INCREMENT,
        TEXT VARCHAR(512) NOT NULL,
        DATE DATETIME,
        UNIQUE KEY id (id)
    );";
    
    $wpdb->query($structure);
 
    // Populate table
    $wpdb->query("INSERT INTO $table(TEXT)
        VALUES('Make sure the material you are uploading does not violate the company\'s rules regarding adequacy of content.')");
    $wpdb->query("INSERT INTO $table(TEXT)
        VALUES('Uploaded Material is available to any contributor within a project and can be copied and used for any company related work. The company is the copyright owner of all the material in this reposistory.')");
    $wpdb->query("INSERT INTO $table(TEXT)
        VALUES('It is the contributors responsibility to only upload material they produced for the company or they have otherwise obtained the copyright for the material.')");
     
}
add_action('activate_syn_rules/syn_user_rules.php', 'rules_install');


// ADMINISTRATOR FUNCTIONALITY
// add the admin menu
add_action('admin_menu', 'syn_rules_menu');

function syn_rules_menu() {
  add_menu_page('Rules', 'Rules', 'manage_options', 'syn_rules', 'edit_rules');
}

// Create the page to edit the rules
function edit_rules() {
	global $wpdb;
	$editId = -1;
	
?>
<script type="text/javascript">
	function rule(id, action, id2) {
		if (action == "syn_rules_delete") {
			if (!confirm("Are you sure you want to delete this rule?") )
     			return;
     	}
		var inputId = document.createElement("INPUT");
		inputId.type = "hidden";
		inputId.value = id;
		inputId.name = action;
		inputId.id = action;
		document.getElementById("hiddenForm").appendChild(inputId);
		if (id2 != 0) {
			var inputId2 = document.createElement("INPUT");
			inputId2.type = "hidden";
			inputId2.value = id2;
			inputId2.name = action + "2";
			inputId2.id = action + "2";
			document.getElementById("hiddenForm").appendChild(inputId2);
		}
		document.getElementById("hiddenForm").submit();
	}
</script>


<?php	

	eval_input(); // evaluate any $_POST queries that INSERT or UPDATE rules
  echo '<div class="wrap">';
  echo '<h1>RULES</h1>';
  
  if (isset($_POST['syn_rules_edit'])) {
	  
  		$editId = $_POST['syn_rules_edit'];
  		$text = $wpdb->get_var("SELECT `TEXT` FROM `{$wpdb->prefix}user_rules` WHERE `id` = $editId;");
		// echo $text;
  }
  
  if ($editId == -1) {
?>
	<h2>Add a new rule</h2>
	<form method="post" action="">
	  <textarea name="syn_newrule" rows="4" id="syn_newrule" style="width:95%"></textarea><br />
	  <input type='submit' name='syn_rules_add' id='addproj' value='add' />
	</form>
    
<?php } else {  ?>

	<h2>Edit rule</h2>
	<form method="post" action="">
	  <textarea name="syn_rules_edittext" rows="4" style="width:95%"><?php echo $text; ?></textarea>
	  <input type='hidden' name='syn_rules_editid' value=<?php echo $editId; ?> />
 	  <input type='submit' name='syn_rules_changetext' value='submit' />
	</form>
<?php

}
  show_rules();   // Display the current rules
  echo '</div>';
}

// this displays the existing rules below the edit form
function show_rules() {
	global $wpdb;
  
	echo"<p>&nbsp;</p>";
	echo "<h2>Current Rules</h2>\n";

	$result = $wpdb->get_results("SELECT `id`, `TEXT` FROM `{$wpdb->prefix}user_rules` ORDER BY `id`"); 
	echo '<form method="post" action="">' . "\n";
	echo "<ol style=\"padding-left:16px\">\n";
	
	$previd = -1;
	$i = 0;
	foreach ($result as $nextr) {
		if ($i != 0) {
			echo "<li style='padding-bottom:6px'>$r->TEXT<br />\n";
			echo "<a  href='javascript: rule($r->id, \"syn_rules_edit\", 0);' style='font-size:0.9em'>edit</a>\n";
			echo " | <a  href='javascript: rule($r->id, \"syn_rules_delete\", 0);' style='font-size:0.9em'>delete</a>\n";
			if ($i != 1)  echo " | <a  href='javascript: rule($r->id, \"syn_rules_swap\", $previd);' style='font-size:0.9em'>move up</a>\n" ;
			if ($i != $count) echo " | <a  href='javascript: rule($r->id, \"syn_rules_swap\", $nextr->id);' style='font-size:0.9em'>move down </a> </li>\n";
			$previd = $r->id;
		 }
		$r=$nextr;
		$i++;
	}

	echo "<li style='padding-bottom:6px'>$r->TEXT<br />\n";
	echo "<a  href='javascript: rule($r->id, \"syn_rules_edit\", 0);' style='font-size:0.9em'>edit</a>\n";
	echo " | <a  href='javascript: rule($r->id, \"syn_rules_delete\", 0);' style='font-size:0.9em'>delete</a>\n";
	if ($i != 1)  echo " | <a  href='javascript: rule($r->id, \"syn_rules_swap\", $previd);' style='font-size:0.9em'>move up</a>\n" ;

	echo "</ol></form>\n";
	
	// a hidden form to send input via javascript
	echo '</div>';
	echo	'<div style="display: none;">';
	echo	'<form method="post" action="" id="hiddenForm"></form>';
	echo "</div>\n";

}

// this function looks for $_POST and does the UPDATE/INSERT queries
function eval_input() {
	global $wpdb;
	
	// ADD a new rule
	if(isset($_POST[syn_rules_add]) && $_POST[syn_rules_add] == "add") {
		$text = $_POST['syn_newrule'];
		if ($text != "") {
			$wpdb->query("INSERT INTO `{$wpdb->prefix}user_rules`(TEXT, DATE)   
						VALUES('$text', NOW())");
		}
 	}
	
	// CHANGE/EDIT a rule
	else if(isset($_POST['syn_rules_changetext'])) {
		$id = $_POST['syn_rules_editid'];
		$text = $_POST['syn_rules_edittext'];
		if ($text != "") {
			$wpdb->query("UPDATE `{$wpdb->prefix}user_rules` 
						  SET `TEXT` = '$text', `DATE` = NOW() WHERE `id` = $id;");
						
		}
	}
	
	// DELETE A RULE (javascript has asked for confirmation)
	else if(isset($_POST['syn_rules_delete'])) {
		$id = $_POST['syn_rules_delete'];
		$wpdb->query("DELETE from `{$wpdb->prefix}user_rules` WHERE `id` = $id;");
	}

	// SWAP TWO RULES (javascript has asked for confirmation)
	else if(isset($_POST['syn_rules_swap'])) {
		$id = $_POST['syn_rules_swap'];
		$id2 = $_POST['syn_rules_swap2'];
		$wpdb->query("UPDATE `{$wpdb->prefix}user_rules` SET `id` = -1 WHERE `id` = $id");
		$wpdb->query("UPDATE `{$wpdb->prefix}user_rules` SET `id` = $id WHERE `id` = $id2");
		$wpdb->query("UPDATE `{$wpdb->prefix}user_rules` SET `id` = $id2 WHERE `id` = -1");						
	}

}

?>